package easy;

public class Solution_1957 {

    public String makeFancyString(String s) {
        StringBuffer buffer = new StringBuffer();
        char[] chars = s.toCharArray();
        char pre = ' ';
        for (int i = 0, count = 0; i < chars.length; i++) {
            if (chars[i] == pre) {
                count++;
            } else {
                for (int j = 0; j < Math.min(count, 2); j++) {
                    buffer.append(pre);
                }
                count = 1;
                pre = chars[i];
            }
            if (i == chars.length - 1) {
                for (int j = 0; j < Math.min(count, 2); j++) {
                    buffer.append(pre);
                }
            }
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        Solution_1957 model = new Solution_1957();
        System.out.println(model.makeFancyString("leeetcode"));
        System.out.println(model.makeFancyString("aaabaaaa"));
        System.out.println(model.makeFancyString("aab"));
        System.out.println(model.makeFancyString("a"));
    }
}
